from django.shortcuts import render
from django.views import View
from .models import Animalinfo
import json
from django.http import HttpResponse
# Create your views here.

class IndexView(View):

    def home(request):
        animals = Animalinfo.objects.all()
        content = {
            'animals' : animals
        }
        # c = {}
        # b = {}
        # for animal in animals:
        #     a_id = animal.id
            # content =  {
            #     'a_name': animal.animal_name,
            #     'a_detail': animal.animal_detail,
            #     'a_env': animal.animal_env
            # }


        return render(request,'index.html',content)

    def animals(request):
        # animals = Animalinfo.objects.all()
        animal_list = []
        for animal_info in Animalinfo.objects.all():
            animal_list.append({
                # 'det': 'null',
                'id': animal_info.id,
                'animal_name': animal_info.animal_name,
                'animal_detail': animal_info.animal_detail,
                'animal_env': animal_info.animal_env,
                'animal_facility': animal_info.animal_facility,
                'animal_bio': animal_info.animal_bio,
                'animal_supplier': animal_info.animal_supplier,
                'animal_num' : animal_info.animal_num
            })
        animal_dic = {}
        animal_dic['data'] = animal_list  # 格式一定要符合官网的json格式，否则会出现一系列错误
        return HttpResponse(json.dumps(animal_dic))
    def datatb(request):
        return render(request,'datatb.html')
